package com.xtiim.battery.discharge.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 *
 * BigDecimal Util
 */
public class BigDecimalUtil {

    /**
     * handle capacity
     * @param capacity
     * @return
     */
    public static Double handleCapacity(Double capacity) {
        if (capacity == null) {
            return null;
        }
        return new BigDecimal(capacity.toString()).setScale(2, RoundingMode.HALF_UP).doubleValue();
    }

    /**
     *
     * handle double data
     * @param data
     * @param scale
     * @return
     */
    public static String handleDoubleData(Double data, int scale) {
        if (data == null) {
            return null;
        }
        return new BigDecimal(data.toString()).setScale(scale, BigDecimal.ROUND_DOWN).toPlainString();
    }

    public static Double handleDouble(Double data, int scale) {
        if (data == null) {
            return null;
        }
        return new BigDecimal(data.toString()).setScale(scale, BigDecimal.ROUND_DOWN).doubleValue();
    }

    /**
     * handle double data, scale default 3
     * @param data
     * @return
     */
    public static String handleDoubleData(Double data) {
        if (data == null) {
            return null;
        }
        return new BigDecimal(data.toString()).setScale(3, BigDecimal.ROUND_DOWN).toPlainString();
    }


    public static void main(String[] args) {
        Double d = 0.0000001;
        System.out.println(new BigDecimal(d.toString()).toString());
        System.out.println(new BigDecimal(d.toString()).toPlainString());
        System.out.println(new BigDecimal(d.toString()).toEngineeringString());
    }

}
